#include "StdAfx.h"
#include "Task.h"
#include "MD5Check.h"
#include <shlobj.h>
#include <shlwapi.h>
#pragma comment(lib,"shell32.lib")
#pragma comment(lib,"shlwapi.lib")

CTask::CTask(void):m_nState(DOWN_INIT),m_pDownloadThread(NULL),m_pTaskCallBack(NULL),m_pNeedSetup(NULL),
m_bIsPause(false)
{
}

CTask::~CTask(void)
{
	if(m_pDownloadThread)
	{
		Stop();
		delete m_pDownloadThread;
		m_pDownloadThread = NULL;
	}
}

CMaterialInfo CTask::GetTaskContext()
{
	return m_TaskInfomation;
}

void CTask::Run(TASKTYPE nTaskType,ITaskEvent* pNetEvent,CSetupThread* pSetup,CPostCount* pPostCount)
{
	m_bIsPause = false;

	m_pTaskCallBack = pNetEvent;
	m_nTaskType = nTaskType;

	m_pNeedSetup = pSetup;
	m_pPostCount = pPostCount;

	TCHAR szPath[MAX_PATH] ={0};
	GetTempPath(MAX_PATH, szPath);
	//GetTempFileName(szPath, NULL,0,szPath);
	PathAddBackslash(szPath);
	_tcscat(szPath,L"Storm\\");
	//MessageBox(NULL,szPath,L"",MB_OK);

	CreateDirectory(szPath,NULL);

	CStdString strDiskPath = szPath;
	m_strSetupPath = strDiskPath + m_TaskInfomation.m_strProcess;
	/*if(m_pPostCount && m_TaskInfomation.m_strID != L"")
	{
		CStdString strPostCount = MATERIAL_POSTCONT_DOWNLOAD_ADDRESS;
		strPostCount += m_TaskInfomation.m_strID;
		strPostCount += MATERIAL_POSTCONT_TYPE;
		m_pPostCount->AddPostCount(strPostCount.GetData());
	}*/
	if(m_pDownloadThread == NULL)
		m_pDownloadThread = new CDownloadThread(nTaskType,m_TaskInfomation.m_strUrl,m_strSetupPath);
	_ASSERT(m_pDownloadThread);
	if(m_pDownloadThread)
		m_pDownloadThread->Start(this);

}

void CTask::Pause()
{
	m_bIsPause = true;
	if(m_pDownloadThread)
	{
		m_pDownloadThread->Cancel();
	}
}

void CTask::Stop()
{
	if(m_pDownloadThread)
	{
		m_pDownloadThread->Cancel();
	}
}

TASKSTATE CTask::GetState()
{
	return m_nState;
}

void CTask::SetTaskContent(const CMaterialInfo& content)
{
	m_TaskInfomation = content;
}

void __stdcall CTask::OnWinNetError(DWORD dwCode, LPCTSTR lpError)
{
	if(m_pTaskCallBack && m_bIsPause == false)
		m_pTaskCallBack->OnErrorCallBack(m_TaskInfomation.m_strID,dwCode,lpError);
}

void __stdcall CTask::OnProgress(long long dwLength, long long dwTotalSize)
{
	if(m_pTaskCallBack && m_bIsPause == false)
		m_pTaskCallBack->OnProcessCallBack(m_TaskInfomation.m_strID,dwLength,dwTotalSize);
}

void __stdcall CTask::OnDownloadComplete(BOOL bSuccessed)
{
	BOOL bIsMD5Check = bSuccessed;
	if(m_TaskInfomation.m_strMD5 != L"")
	{
		CMD5Check cCheck;
		bIsMD5Check = cCheck.CheckMD5(m_strSetupPath,0,0,m_TaskInfomation.m_strMD5);
	}
	if(m_pTaskCallBack && m_bIsPause == false)
		m_pTaskCallBack->OnDownloadComplete(m_TaskInfomation.m_strID,bIsMD5Check);
	if(bIsMD5Check == TRUE && m_pNeedSetup)
	{
		/*if(m_pPostCount)
		{
			CStdString strUrl = MATERIAL_POSTCONT_SETUP_ADDRESS;
			strUrl += m_TaskInfomation.m_strID;
			strUrl += MATERIAL_POSTCONT_TYPE;
			m_pPostCount->AddPostCount(strUrl.GetData());
		}*/
		m_pNeedSetup->AddSetupTask(m_TaskInfomation.m_strID,m_strSetupPath);
	}

}